package com.sk.ccflower.server.messenger;

import javax.jms.MessageConsumer;
import javax.jms.ObjectMessage;

import org.apache.log4j.Logger;

import com.sk.ccflower.lib.commonUtils.ObjectUtil;
import com.sk.ccflower.lib.define.BaseCfg;
import com.sk.ccflower.lib.define.Consts;
import com.sk.ccflower.lib.meBeans.MessageBean;
import com.sk.ccflower.server.lib.dbBeans.MfCommentBean;
import com.sk.ccflower.server.lib.dbUtils.qry.MfCommentQry;
import com.sun.messaging.ConnectionConfiguration;
import com.sun.messaging.ConnectionFactory;
import com.sun.messaging.Destination;
import com.sun.messaging.jms.Connection;
import com.sun.messaging.jms.JMSException;
import com.sun.messaging.jms.Message;
import com.sun.messaging.jms.Session;

public class ChatQueueThread extends Thread {
	
	private Logger m_log;
	private ConnectionFactory m_fac = null;
	private Connection m_connection = null;
	private Session m_session = null;
	private Destination m_dest = null;
	private MessageConsumer m_consumer = null;
	private MessengerCenter m_parent = null;
	
	public ChatQueueThread(MessengerCenter _parent) {
		this.m_log = Logger.getLogger( getClass() );
		this.m_parent = _parent;
	}
	
	private void connectBroker() {
		try {
			m_fac = new ConnectionFactory();
			m_fac.setProperty( ConnectionConfiguration.imqAddressList, BaseCfg.MESSAGE_QUEUE_SERVER_URL );
			m_fac.setProperty( ConnectionConfiguration.imqReconnectEnabled, "true" );
			
			m_connection = (Connection) m_fac.createConnection();
			
			m_session = (Session) m_connection.createSession( false, Session.AUTO_ACKNOWLEDGE );
			
			m_dest = new com.sun.messaging.Queue( Consts.MESSAGE_QUEUE_NAME.MESSENGER_CHAT );
			
			m_consumer = m_session.createConsumer( m_dest );
			
			m_connection.start();
		} catch ( Exception e ) {
			try {
				Thread.sleep( 1000 );
			} catch ( Exception e1 ) {
				
			}
			connectBroker();
		}
	}
	
	private void save2DB( MessageBean bean ) throws Exception {
		java.sql.Connection conn = null;
		try {
			MfCommentBean dbBean = new MfCommentBean();
			ObjectUtil.objectCopy( dbBean, bean, false );
			dbBean.confirm = 0;
			
			conn = (java.sql.Connection) m_parent.connectionPool.getConnection();
			MfCommentQry qry = new MfCommentQry(conn);
			qry.insert( dbBean );
		} catch ( Exception e ) {
			this.m_log.error( e );
		} finally {
			if ( null != conn ) {
				conn.close();
			}
		}
	}
	
	public void run() {
		connectBroker();
		MessageBean bean = null;
		
		while ( true ) {
			try {
				Message inMsg = (Message) m_consumer.receive();
				ObjectMessage objMsg = (ObjectMessage) inMsg;
				
				bean = (MessageBean) objMsg.getObject();
				if ( null != bean ) {
					save2DB( bean );
				}
				
				Thread.sleep( 10 );
			} catch ( JMSException e ) {
				this.m_log.error( e );
				try {
					if  ( null != m_session )		m_session.close();
					if  ( null != m_connection )	m_connection.close();
					
					connectBroker();
				} catch ( Exception e1 ) {
					this.m_log.error( e1 );
				}
			} catch ( Exception e ) {
				this.m_log.error( e );
			}
		}
	}
	
}
